/**
 * 用户列表功能
 * @type {account_list}
 */
var account_list = function () {
	var grid_id = 'account_list';
    //用户列表操作栏图标操作
    var operation_icon=[
        {id:"edit",icon_url:"/edit.gif",description:language.common.update},
        {id:"remove",icon_url:"/remove.gif",description:language.common.remove}
    ];
	var operation_formatter = function operationFormatter(item, index) {
       var template = '<a href="edit/{0}"><img src="' +js_param.image_url +operation_icon[0].icon_url+'"title="'+operation_icon[0].description+'"height="16" width="16" border="0"/></a>' +
            ' &nbsp;&nbsp;<a href="#" onclick="account_list.del({0})"><img src="' + js_param.image_url +operation_icon[1].icon_url+'"title="'+operation_icon[1].description+'"height="16" width="16" border="0"/></a>';
		return template.format(item.id, language.common.update,language.common.remove);
	};
	return {
		/**
		 * 显示列表
		 */
		list        :function () {
            cdms.common.jart_grid(grid_id, {
                sortField:'login', url:"list.json", idFields:'id', gridline:'horizontal',showCheckBox:'false',
                columns        :[
                    [
                        { title:language.account.column_1, field:"login",headAlign:'left',width:"300px",sortfield: "login"},
                        { title:language.account.column_2, field:"mail",headAlign:'left'},
                        { title:language.account.column_3, field:"name",headAlign:'left', width:"130px"},
                        { title:language.common.operation, field:"operation",headAlign:'center',align:'center',width:"130px", formatter:operation_formatter }
                    ]
                ],
                //选中触发事件
                onCheck        :function () {
                        var rows = $('#' + grid_id).grid("checkedRows");
                        if (rows.length == 0) {
                            $('#account_toolbar').toolbar().litems(1).attr("disabled", true);
                            return;
                        }
                    $('#account_toolbar').toolbar().litems(1).attr("disabled", false);
                }
            });
		},
		/**
		 * 跳转到新增
		 */
		add         :function () {
			window.location.href = "edit/0";
		},
		/**
		 * 删除操作
		 */
		remove      :function () {
			var selectRowId = cdms.common.grid_select_ids(grid_id);
			if (selectRowId == undefined) {
				return;
			}
			cdms.common.confirm(language.account.remove_tip_msg, function () {
				cdms.common.ajax_post('delete', {ids:selectRowId}, account_list.reload);
			});
		},
		//删除单个用户
		del         :function (id) {
			cdms.common.confirm(language.account.remove_tip_msg, function () {
				cdms.common.ajax_post('delete', {ids:id}, account_list.reload);
                $('#' + grid_id).grid().loadData();
			});
		},
		//检索
		fuzzy_search:function () {
			var search = $('#search').val();
			var params = {'search':search};
			$('#' + grid_id).grid({
				url        :"fuzzy_search",
				queryParams:params,
				pageindex  :1
			});

			account_list.reload();
		},
		/**
		 * 刷新表格
		 */
		reload      :function () {
			$('#' + grid_id).grid().loadData();
		},
		/**
		 * 上传文件
		 */
		importTeam      : function () {
			$('#upload_file').layer({title: language.account.import_exl_data, target: "top"}).open();
			//文件上传
			cdms.common.form_submit('import_form', {}, function (data) {
				if (data.type) {
                    $.alert(data.msg, language.note.alert_title);
					cdms.common.show_ajax_msg(data.msg);
					account_list.closeImport();
					account_list.reload();
				} else {
					$.alert(data.msg, language.note.alert_title);
				}
			});
			$('#upload_file_cancel').click(function () {
				account_list.closeImport();
			})
		},
		closeImport : function () {
			$('#upload_file').layer().close();
		}

	};
}();


$(document).ready(function () {

	account_list.list();
	$('#search').textbox().event("change",account_list.fuzzy_search);
});